package com.social.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

import javax.annotation.Resource;

import static com.social.constant.RabbitMqConstants.*;

@Configuration
public class RabbitMQConfig implements RabbitListenerConfigurer {


    @Bean("activityExchange")
    public DirectExchange activityExchange(){
        return ExchangeBuilder.directExchange(ACTIVITY_EXCHANGE).durable(true).build();
    }

    @Bean("deadExchange")
    public DirectExchange deadExchange() {
        return ExchangeBuilder.directExchange(DEAD_DIRECT_EXCHANGE).durable(true).build();
    }

    @Bean("activityQueue")
    public Queue activityQueue(){
        return QueueBuilder.durable(ACTIVITY_REGISTER_QUEUE)
                .withArgument("x-dead-letter-exchange",DEAD_DIRECT_EXCHANGE) //死信交换机
                .withArgument("x-dead-letter-routing-key",DEAD_ACTIVITY_REGISTER_QUEUE).build(); //死信routing
    }

    @Bean("deadActivityQueue")
    public Queue deadActivityQueue(){
        return QueueBuilder.durable(DEAD_ACTIVITY_REGISTER_QUEUE).build();
    }


    @Bean("volunteerQueue")
    public Queue volunteerQueue(){
        return QueueBuilder.durable(VOLUNTEER_REGISTER_QUEUE).build();
    }

    @Bean
    public Binding bindingDeaDActivityQueueExchange(@Qualifier("deadActivityQueue") Queue queue, @Qualifier("deadExchange") DirectExchange exchange){
        /**
            * @Description 绑定死信队列和交换机
        */
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_ACTIVITY_REGISTER_QUEUE);
    }
    @Bean
    public Binding bindingActivityQueueExchange(@Qualifier("activityQueue") Queue queue, @Qualifier("activityExchange") DirectExchange exchange){
        /**
         * @className: RabbitMQConfig
         * @description: TODO 绑定队列和交换机
         * @author: XiaoXu
         * @date: 2022/11/18
         **/
        return BindingBuilder.bind(queue).to(exchange).with(ACTIVITY_REGISTER_ROUTING);
    }

    @Bean
    public Binding bindingVolunteerQueueExchange(@Qualifier("volunteerQueue") Queue queue, @Qualifier("activityExchange") DirectExchange exchange){
        /**
         * @className: RabbitMQConfig
         * @description: TODO 绑定队列和交换机
         * @author: XiaoXu
         * @date: 2022/11/18
         **/
        return BindingBuilder.bind(queue).to(exchange).with(VOLUNTEER_REGISTER_ROUTING);
    }

    @Bean("delayExchange")
    public DirectExchange delayExchange() {
        return ExchangeBuilder.directExchange(DELAY_EXCHANGE).durable(true).build();
    }
    @Bean("delayQueue")
    public Queue delayQueue(){
        return QueueBuilder.durable(DELAY_QUEUE).build();
    }

    @Bean
    public Binding bindingDelayQueueExchange(@Qualifier("delayQueue") Queue queue, @Qualifier("delayExchange") DirectExchange exchange){
        /**
         * @className: RabbitMQConfig
         * @description: TODO 绑定队列和交换机
         * @author: XiaoXu
         * @date: 2022/11/18
         **/
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_EMAIL_ROUNTING);
    }

    // 可以将json串反序列化为对象
    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
        rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
    }

    @Bean
    MessageHandlerMethodFactory messageHandlerMethodFactory(){
        DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
        messageHandlerMethodFactory.setMessageConverter(mappingJackson2MessageConverter());
        return messageHandlerMethodFactory;
    }

    @Bean
    public MappingJackson2MessageConverter mappingJackson2MessageConverter(){
        return  new MappingJackson2MessageConverter();
    }

    // 提供自定义RabbitTemplate,将对象序列化为json串
    @Bean
    public RabbitTemplate jacksonRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
}
